using System;
 using System.Collections.Generic;
 using System.Text;
 using System.Data;
 using System.Data.SqlClient;
 using MinhTam.DataAccess.Web;
//───────────────────────────────────────────────────────────────────────────────────────
 namespace MinhTam.Objects
  { 
 public class LOG_IN_SESSION:  SQLConnectWeb { 
 public static string sTableName= "LOG_IN_SESSION"; 
   public string SESSION_ID;
   public string CLIENT_ID;
   public string USER_ID;
   public string USER_NAME;
   public string FULL_NAME;
   public string CLIENT_DATE;
   public string USER_GROUP_ID;
   #region DataColumn Name ;
 public static  string cl_SESSION_ID="SESSION_ID" ;
 public static  string cl_CLIENT_ID="CLIENT_ID" ;
 public static  string cl_USER_ID="USER_ID" ;
 public static  string cl_USER_NAME="USER_NAME" ;
 public static  string cl_FULL_NAME="FULL_NAME" ;
 public static  string cl_CLIENT_DATE="CLIENT_DATE" ;
 public static  string cl_USER_GROUP_ID="USER_GROUP_ID" ;
 #endregion;
//───────────────────────────────────────────────────────────────────────────────────────
       public LOG_IN_SESSION() {}
//───────────────────────────────────────────────────────────────────────────────────────
       public LOG_IN_SESSION(
         string sSESSION_ID,
         string sCLIENT_ID,
         string sUSER_ID,
         string sUSER_NAME,
         string sFULL_NAME,
         string sCLIENT_DATE,
         string sUSER_GROUP_ID){
         this.SESSION_ID= sSESSION_ID;
         this.CLIENT_ID= sCLIENT_ID;
         this.USER_ID= sUSER_ID;
         this.USER_NAME= sUSER_NAME;
         this.FULL_NAME= sFULL_NAME;
         this.CLIENT_DATE= sCLIENT_DATE;
         this.USER_GROUP_ID= sUSER_GROUP_ID;
}
//───────────────────────────────────────────────────────────────────────────────────────
       public static LOG_IN_SESSION Create_LOG_IN_SESSION ( string sSESSION_ID  ){
    DataTable dt=SearchBySESSION_ID(sSESSION_ID) ;
    if(dt!=null && dt.Rows.Count>0) 
      return new LOG_IN_SESSION(dt,0);
      return null;
}
//───────────────────────────────────────────────────────────────────────────────────────
   private static string s_Select()
    {
   return " SELECT T.* FROM LOG_IN_SESSION AS T";
    }
//───────────────────────────────────────────────────────────────────────────────────────
 public LOG_IN_SESSION( DataTable table,int pos)
{
         this.SESSION_ID= table.Rows[pos]["SESSION_ID"].ToString();
         this.CLIENT_ID= table.Rows[pos]["CLIENT_ID"].ToString();
         this.USER_ID= table.Rows[pos]["USER_ID"].ToString();
         this.USER_NAME= table.Rows[pos]["USER_NAME"].ToString();
         this.FULL_NAME= table.Rows[pos]["FULL_NAME"].ToString();
         this.CLIENT_DATE= table.Rows[pos]["CLIENT_DATE"].ToString();
         this.USER_GROUP_ID= table.Rows[pos]["USER_GROUP_ID"].ToString();
}
//───────────────────────────────────────────────────────────────────────────────────────
 public static DataTable SearchBySESSION_ID(string sSESSION_ID)
{
          string sqlSelect= s_Select()+ " WHERE SESSION_ID  Like '%"+ sSESSION_ID + "%'"; 
          DataTable dt=GetTable(sqlSelect,sTableName) ;
          return dt; 
 }//───────────────────────────────────────────────────────────────────────────────────────
 public static DataTable SearchByCLIENT_ID(string sCLIENT_ID)
{
          string sqlSelect= s_Select()+ " WHERE CLIENT_ID  Like N'%"+ sCLIENT_ID + "%'"; 
          DataTable dt=GetTable(sqlSelect,sTableName) ;
          return dt; 
 }//───────────────────────────────────────────────────────────────────────────────────────
 public static DataTable SearchByUSER_ID(string sUSER_ID)
{
          string sqlSelect= s_Select()+ " WHERE USER_ID  ="+ sUSER_ID + ""; 
          DataTable dt=GetTable(sqlSelect,sTableName) ;
          return dt; 
 }//───────────────────────────────────────────────────────────────────────────────────────
//───────────────────────────────────────────────────────────────────────────────────────
 public static DataTable SearchByUSER_ID(string sUSER_ID,string sMatch)
{
          string sqlSelect= s_Select()+ " WHERE USER_ID"+ sMatch +sUSER_ID + ""; 
          DataTable dt=GetTable(sqlSelect,sTableName) ;
          return dt; 
 }//───────────────────────────────────────────────────────────────────────────────────────
 public static DataTable SearchByUSER_NAME(string sUSER_NAME)
{
          string sqlSelect= s_Select()+ " WHERE USER_NAME  Like N'%"+ sUSER_NAME + "%'"; 
          DataTable dt=GetTable(sqlSelect,sTableName) ;
          return dt; 
 }//───────────────────────────────────────────────────────────────────────────────────────
 public static DataTable SearchByFULL_NAME(string sFULL_NAME)
{
          string sqlSelect= s_Select()+ " WHERE FULL_NAME  Like N'%"+ sFULL_NAME + "%'"; 
          DataTable dt=GetTable(sqlSelect,sTableName) ;
          return dt; 
 }//───────────────────────────────────────────────────────────────────────────────────────
 public static DataTable SearchByCLIENT_DATE(string sCLIENT_DATE)
{
          string sqlSelect= s_Select()+ " WHERE CLIENT_DATE  ="+ sCLIENT_DATE + ""; 
          DataTable dt=GetTable(sqlSelect,sTableName) ;
          return dt; 
 }//───────────────────────────────────────────────────────────────────────────────────────
//───────────────────────────────────────────────────────────────────────────────────────
 public static DataTable SearchByCLIENT_DATE(string sCLIENT_DATE,string sMatch)
{
          string sqlSelect= s_Select()+ " WHERE CLIENT_DATE"+ sMatch +sCLIENT_DATE + ""; 
          DataTable dt=GetTable(sqlSelect,sTableName) ;
          return dt; 
 }//───────────────────────────────────────────────────────────────────────────────────────
 public static DataTable SearchByUSER_GROUP_ID(string sUSER_GROUP_ID)
{
          string sqlSelect= s_Select()+ " WHERE USER_GROUP_ID  ="+ sUSER_GROUP_ID + ""; 
          DataTable dt=GetTable(sqlSelect,sTableName) ;
          return dt; 
 }//───────────────────────────────────────────────────────────────────────────────────────
//───────────────────────────────────────────────────────────────────────────────────────
 public static DataTable SearchByUSER_GROUP_ID(string sUSER_GROUP_ID,string sMatch)
{
          string sqlSelect= s_Select()+ " WHERE USER_GROUP_ID"+ sMatch +sUSER_GROUP_ID + ""; 
          DataTable dt=GetTable(sqlSelect,sTableName) ;
          return dt; 
 }//───────────────────────────────────────────────────────────────────────────────────────
 public static DataTable Search( string sSESSION_ID
            , string sCLIENT_ID
            , string sUSER_ID
            , string sUSER_NAME
            , string sFULL_NAME
            , string sCLIENT_DATE
            , string sUSER_GROUP_ID
            )
 {
       string sqlselect=s_Select() + " WHERE" ;
      if (sSESSION_ID!=null && sSESSION_ID!="") 
            sqlselect +=" AND SESSION_ID LIKE '%" + sSESSION_ID +"%'" ;
      if (sCLIENT_ID!=null && sCLIENT_ID!="") 
            sqlselect +=" AND CLIENT_ID LIKE N'%" + sCLIENT_ID +"%'" ;
      if (sUSER_ID!=null && sUSER_ID!="") 
            sqlselect +=" AND USER_ID =" + sUSER_ID ;
      if (sUSER_NAME!=null && sUSER_NAME!="") 
            sqlselect +=" AND USER_NAME LIKE N'%" + sUSER_NAME +"%'" ;
      if (sFULL_NAME!=null && sFULL_NAME!="") 
            sqlselect +=" AND FULL_NAME LIKE N'%" + sFULL_NAME +"%'" ;
      if (sCLIENT_DATE!=null && sCLIENT_DATE!="") 
            sqlselect +=" AND CLIENT_DATE LIKE '%" + sCLIENT_DATE +"%'" ;
      if (sUSER_GROUP_ID!=null && sUSER_GROUP_ID!="") 
            sqlselect +=" AND USER_GROUP_ID =" + sUSER_GROUP_ID ;
   sqlselect=sqlselect.Replace("WHERE AND","WHERE");
   int n=sqlselect.IndexOf("WHERE");
   if(n==sqlselect.Length -5) sqlselect=sqlselect.Remove(n,5) ;
   return GetTable(sqlselect,sTableName);
}
//───────────────────────────────────────────────────────────────────────────────────────
 public static LOG_IN_SESSION Insert_Object(
string sCLIENT_ID
            , string sUSER_ID
            , string sUSER_NAME
            , string sFULL_NAME
            , string sUSER_GROUP_ID
            )
 {
     string tem_sCLIENT_ID = MinhTam.DataAccess.Web.SQLToolWeb.GetSaveValue(sCLIENT_ID, "nvarchar");
     string tem_sUSER_ID = MinhTam.DataAccess.Web.SQLToolWeb.GetSaveValue(sUSER_ID, "int");
     string tem_sUSER_NAME = MinhTam.DataAccess.Web.SQLToolWeb.GetSaveValue(sUSER_NAME, "nvarchar");
     string tem_sFULL_NAME = MinhTam.DataAccess.Web.SQLToolWeb.GetSaveValue(sFULL_NAME, "nvarchar");

     string tem_sUSER_GROUP_ID = MinhTam.DataAccess.Web.SQLToolWeb.GetSaveValue(sUSER_GROUP_ID, "int");

     string sqlSave = " INSERT INTO LOG_IN_SESSION(" +
           "CLIENT_ID,"
+ "USER_ID,"
+ "USER_NAME,"
+ "FULL_NAME,"
+ "CLIENT_DATE,"
+ "USER_GROUP_ID) VALUES("
+ tem_sCLIENT_ID + ","
+ tem_sUSER_ID + ","
+ tem_sUSER_NAME + ","
+ tem_sFULL_NAME + ","
+ "getDate()," 
+tem_sUSER_GROUP_ID + ")";
     bool OK = Exec(sqlSave) >= 1 ? true : false;
     if (OK)
     {
         LOG_IN_SESSION newLOG_IN_SESSION = new LOG_IN_SESSION();
         DataTable dt = GetTable(" SELECT TOP 1 SESSION_ID,CLIENT_DATE FROM LOG_IN_SESSION ORDER BY SESSION_ID DESC ");
         newLOG_IN_SESSION.SESSION_ID = dt.Rows[0]["SESSION_ID"].ToString();
         newLOG_IN_SESSION.CLIENT_ID = sCLIENT_ID;
         newLOG_IN_SESSION.USER_ID = sUSER_ID;
         newLOG_IN_SESSION.USER_NAME = sUSER_NAME;
         newLOG_IN_SESSION.FULL_NAME = sFULL_NAME;
         newLOG_IN_SESSION.CLIENT_DATE = dt.Rows[0]["CLIENT_DATE"].ToString();
         newLOG_IN_SESSION.USER_GROUP_ID = sUSER_GROUP_ID;
         return newLOG_IN_SESSION;
     }
     else return null;
 }
//─────────────────────────────────────────────────────────────────────────────────────
public bool  Save_Object(string sCLIENT_ID
                ,string sUSER_ID
                ,string sUSER_NAME
                ,string sFULL_NAME               
                ,string sUSER_GROUP_ID
                ) 
 { 
              string tem_sCLIENT_ID=MinhTam.DataAccess.Web.SQLToolWeb.GetSaveValue(sCLIENT_ID,"nvarchar");
              string tem_sUSER_ID=MinhTam.DataAccess.Web.SQLToolWeb.GetSaveValue(sUSER_ID,"int");
              string tem_sUSER_NAME=MinhTam.DataAccess.Web.SQLToolWeb.GetSaveValue(sUSER_NAME,"nvarchar");
              string tem_sFULL_NAME=MinhTam.DataAccess.Web.SQLToolWeb.GetSaveValue(sFULL_NAME,"nvarchar");
           
              string tem_sUSER_GROUP_ID=MinhTam.DataAccess.Web.SQLToolWeb.GetSaveValue(sUSER_GROUP_ID,"int");

 string sqlSave=" UPDATE LOG_IN_SESSION SET "+"CLIENT_ID ="+tem_sCLIENT_ID+","
 +"USER_ID ="+tem_sUSER_ID+","
 +"USER_NAME ="+tem_sUSER_NAME+","
 +"FULL_NAME ="+tem_sFULL_NAME+","
 +"CLIENT_DATE = getDate(),"
 +"USER_GROUP_ID ="+tem_sUSER_GROUP_ID+" WHERE SESSION_ID="+MinhTam.DataAccess.Web.SQLToolWeb.GetSaveValue(this.SESSION_ID,"bigint identity");;
              bool OK = Exec(sqlSave)>=1?true:false;
           if (OK) 
           { 
                this.CLIENT_ID=sCLIENT_ID;
                this.USER_ID=sUSER_ID;
                this.USER_NAME=sUSER_NAME;
                this.FULL_NAME=sFULL_NAME;                
                this.USER_GROUP_ID=sUSER_GROUP_ID;
           } 
 return OK;  }
//───────────────────────────────────────────────────────────────────────────────────────
 #region Update DataColumn  
 public bool Update_SESSION_ID(string sSESSION_ID)
{
    string sqlSave= " UPDATE LOG_IN_SESSION SET SESSION_ID='"+ sSESSION_ID+ "' WHERE SESSION_ID='"+ this.SESSION_ID+"' ";
 bool OK=Exec(sqlSave)>=1?true:false;
 if(OK)
 {
    this.SESSION_ID=sSESSION_ID;
 }
 return OK;
}
//───────────────────────────────────────────────────────────────────────────────────────
 public bool Update_CLIENT_ID(string sCLIENT_ID)
{
    string sqlSave= " UPDATE LOG_IN_SESSION SET CLIENT_ID='N"+ sCLIENT_ID+ "' WHERE SESSION_ID='"+ this.SESSION_ID+"' ";
 bool OK=Exec(sqlSave)>=1?true:false;
 if(OK)
 {
    this.CLIENT_ID=sCLIENT_ID;
 }
 return OK;
}
//───────────────────────────────────────────────────────────────────────────────────────
 public bool Update_USER_ID(string sUSER_ID)
{
    string sqlSave= " UPDATE LOG_IN_SESSION SET USER_ID='"+ sUSER_ID+ "' WHERE SESSION_ID='"+ this.SESSION_ID+"' ";
 bool OK=Exec(sqlSave)>=1?true:false;
 if(OK)
 {
    this.USER_ID=sUSER_ID;
 }
 return OK;
}
//───────────────────────────────────────────────────────────────────────────────────────
 public bool Update_USER_NAME(string sUSER_NAME)
{
    string sqlSave= " UPDATE LOG_IN_SESSION SET USER_NAME='N"+ sUSER_NAME+ "' WHERE SESSION_ID='"+ this.SESSION_ID+"' ";
 bool OK=Exec(sqlSave)>=1?true:false;
 if(OK)
 {
    this.USER_NAME=sUSER_NAME;
 }
 return OK;
}
//───────────────────────────────────────────────────────────────────────────────────────
 public bool Update_FULL_NAME(string sFULL_NAME)
{
    string sqlSave= " UPDATE LOG_IN_SESSION SET FULL_NAME='N"+ sFULL_NAME+ "' WHERE SESSION_ID='"+ this.SESSION_ID+"' ";
 bool OK=Exec(sqlSave)>=1?true:false;
 if(OK)
 {
    this.FULL_NAME=sFULL_NAME;
 }
 return OK;
}
//───────────────────────────────────────────────────────────────────────────────────────
 public bool Update_CLIENT_DATE(string sCLIENT_DATE)
{
    string sqlSave= " UPDATE LOG_IN_SESSION SET CLIENT_DATE='"+ sCLIENT_DATE+ "' WHERE SESSION_ID='"+ this.SESSION_ID+"' ";
 bool OK=Exec(sqlSave)>=1?true:false;
 if(OK)
 {
    this.CLIENT_DATE=sCLIENT_DATE;
 }
 return OK;
}
//───────────────────────────────────────────────────────────────────────────────────────
 public bool Update_USER_GROUP_ID(string sUSER_GROUP_ID)
{
    string sqlSave= " UPDATE LOG_IN_SESSION SET USER_GROUP_ID='"+ sUSER_GROUP_ID+ "' WHERE SESSION_ID='"+ this.SESSION_ID+"' ";
 bool OK=Exec(sqlSave)>=1?true:false;
 if(OK)
 {
    this.USER_GROUP_ID=sUSER_GROUP_ID;
 }
 return OK;
}
//───────────────────────────────────────────────────────────────────────────────────────
 #endregion
 #region Update DataColumn  Static 
 public static bool Update_CLIENT_ID(string sCLIENT_ID,string s_SESSION_ID)
{
  string sqlSave= " UPDATE LOG_IN_SESSION SET CLIENT_ID='N"+sCLIENT_ID+"' WHERE SESSION_ID='"+ s_SESSION_ID+"' ";
 bool OK=Exec(sqlSave)>=1?true:false;
 return OK;
}
//───────────────────────────────────────────────────────────────────────────────────────
 public static bool Update_USER_ID(string sUSER_ID,string s_SESSION_ID)
{
  string sqlSave= " UPDATE LOG_IN_SESSION SET USER_ID='"+sUSER_ID+"' WHERE SESSION_ID='"+ s_SESSION_ID+"' ";
 bool OK=Exec(sqlSave)>=1?true:false;
 return OK;
}
//───────────────────────────────────────────────────────────────────────────────────────
 public static bool Update_USER_NAME(string sUSER_NAME,string s_SESSION_ID)
{
  string sqlSave= " UPDATE LOG_IN_SESSION SET USER_NAME='N"+sUSER_NAME+"' WHERE SESSION_ID='"+ s_SESSION_ID+"' ";
 bool OK=Exec(sqlSave)>=1?true:false;
 return OK;
}
//───────────────────────────────────────────────────────────────────────────────────────
 public static bool Update_FULL_NAME(string sFULL_NAME,string s_SESSION_ID)
{
  string sqlSave= " UPDATE LOG_IN_SESSION SET FULL_NAME='N"+sFULL_NAME+"' WHERE SESSION_ID='"+ s_SESSION_ID+"' ";
 bool OK=Exec(sqlSave)>=1?true:false;
 return OK;
}
//───────────────────────────────────────────────────────────────────────────────────────
 public static bool Update_CLIENT_DATE(string sCLIENT_DATE,string s_SESSION_ID)
{
  string sqlSave= " UPDATE LOG_IN_SESSION SET CLIENT_DATE='"+sCLIENT_DATE+"' WHERE SESSION_ID='"+ s_SESSION_ID+"' ";
 bool OK=Exec(sqlSave)>=1?true:false;
 return OK;
}
//───────────────────────────────────────────────────────────────────────────────────────
 public static bool Update_USER_GROUP_ID(string sUSER_GROUP_ID,string s_SESSION_ID)
{
  string sqlSave= " UPDATE LOG_IN_SESSION SET USER_GROUP_ID='"+sUSER_GROUP_ID+"' WHERE SESSION_ID='"+ s_SESSION_ID+"' ";
 bool OK=Exec(sqlSave)>=1?true:false;
 return OK;
}
//───────────────────────────────────────────────────────────────────────────────────────
#endregion
//───────────────────────────────────────────────────────────────────────────────────────
 public static DataTable GetTableAll() 
 {
       return  GetTableAll(null, null);
 }
public static DataTable GetTableAll(string sWhere, params string[] orderFields)
{
   string sqlSelect = " SELECT * FROM LOG_IN_SESSION";
   if (!string.IsNullOrEmpty(sWhere))
      sqlSelect += " where " + sWhere; 
   string order = "";
   if (orderFields != null && orderFields.Length > 0)
     order = string.Join(",", orderFields);
   if (order != "")
      sqlSelect += " ORDER BY " + order;
   return GetTable(sqlSelect,sTableName);
}
//───────────────────────────────────────────────────────────────────────────────────────
//───────────────────────────────────────────────────────────────────────────────────────
public static DataTable GetTableFields(string sWhere, string[] orderFields, params string[] fields)
{
 string field = "";
 if (fields != null && fields.Length > 0)
    field = string.Join(",", fields);
 else field = "*";
 string sqlSelect = string.Format(" SELECT {0} FROM {1} ", field, "LOG_IN_SESSION");
 if (!string.IsNullOrEmpty(sWhere))
    sqlSelect += " where " + sWhere;
 string order = "";
 if (orderFields != null && orderFields.Length > 0)
    order = string.Join(",", orderFields);
 if (order != "")
    sqlSelect += " ORDER BY " + order;
 return GetTable(sqlSelect,sTableName);
 }
 public static DataTable GetTableFields(params string[] fields)
 {
    return GetTableFields(null, null, fields);
 }
 public static DataTable GetTableFields(string[] orderFields, params string[] fields)
 {
    return GetTableFields(null, orderFields, fields);
 }
//───────────────────────────────────────────────────────────────────────────────────────
   private static DataTable dt_LOG_IN_SESSION;
   public static bool Change_dt_LOG_IN_SESSION = true;
   public static bool AllowAutoChange = true;
   public static DataTable get_LOG_IN_SESSION()
   {
   if (dt_LOG_IN_SESSION == null || Change_dt_LOG_IN_SESSION == true)
     {
   dt_LOG_IN_SESSION = GetTableAll();
         Change_dt_LOG_IN_SESSION = true && AllowAutoChange ;
     }
     return dt_LOG_IN_SESSION;
   }
   //───────────────────────────────────────────────────────────────────────────────────────
}  
 } 
